class UnionFind{
    constructor(capacity){
        this.parent = new Array(capacity)
        this.rank = new Array(capacity)
        for(let i=0; i<capacity; i++){
            this.parent[i] = i
            this.rank[i] = i
        }
    }

    find(p){
        while(p != this.parent[p]){
            this.parent[p] = this.parent[this.parent[p]]
            p = this.parent[p]
        }
        return p
    }

    union(p, q){
        let fp = this.find(p)
        let fq = this.find(q)
        if(fp == fq) return
        if(this.rank[fp] < this.rank[fq]){
            this.parent[fp] = fq
        }else if(this.rank[fp] > this.rank[fq]){
            this.parent[fq] = fp
        }else{
            this.parent[fq] = fp
            this.rank[fp] += 1
        }
    }
}

function findCircleNum(isConnected) {
    let row_len = isConnected.length
    let col_len = isConnected[0].length
    let connectedArr = []
    for(let row=0; row<row_len;row++){
        for(let col=0; col<col_len; col++){
            if(isConnected[row][col] ==1 && row < col){
                connectedArr.push([row,col])
            }
        }
    }

    const uf = new UnionFind(row_len)
    connectedArr.forEach((connected) => {
        uf.union(connected[0], connected[1])
    })

    const map = new Map()
    for (let index = 0; index < uf.parent.length; index++) {
        map.set(uf.find(uf.parent[index]), 1)
    }
    console.log(map)
        
   
};

const isConnected = [[1,1,0],[1,1,0],[0,0,1]]
findCircleNum(isConnected)